package com.ehomepay.xyxeample.module.user.controller;
import com.ehomepay.xyxeample.common.base.BaseController;
import com.ehomepay.xyxeample.common.redis.service.RedisClientTemplate;
import com.ehomepay.xyxeample.module.user.entity.User;
import com.ehomepay.xyxeample.module.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ehomepay.xyxeample.common.entity.ResultVo;

import java.util.List;

/**
 * Created by Administrator on 2018/5/7.
 */
@RestController
@Api(value="user-controller")
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisClientTemplate redisService;

    @ApiOperation(value="用户列表",notes = "用户列表")
    @RequestMapping(value={"/user/list"},method= RequestMethod.GET)
    public ResultVo<List> findAll(){
        return super.success(userService.findAll());
    }

    @ApiOperation(value="创建用户",notes = "创建用户")
    @RequestMapping(value={"/user/create"},method= RequestMethod.GET)
    public ResultVo<User> create(@RequestBody User user){
        return super.success(userService.saveUser(user));
    }

    @ApiOperation(value="获取用户信息",notes = "获取用户信息")
    @RequestMapping(value={"/user/findUserById/{id}"},method= RequestMethod.GET)
    public ResultVo<User> create(@PathVariable Long id){
        return super.success(userService.findUserById(id));
    }

    @GetMapping("/user/{username}")
    public ResultVo<User> findUser(@PathVariable("username") String username){

        ResultVo<User> resultVo = new ResultVo<User>();
        resultVo.setCode("200");
        resultVo.setInfo("呵呵");

        User user = userService.findByUsername(username);
        resultVo.setData(user);

        return resultVo;
    }

    @GetMapping("/redis/{username}")
    public ResultVo<User> findRedis(@PathVariable("username") String username){
        redisService.set("1001","1001");
        ResultVo<User> resultVo = new ResultVo<User>();
        resultVo.setCode("200");
        resultVo.setInfo(redisService.get("1001"));
       // User user = userService.findByUsername(username);
       // resultVo.setData(user);
        return resultVo;
    }


    @GetMapping(value = "/test")
    public ResultVo<String> findTest(){
        ResultVo<String> resultVo = new ResultVo<String>();
        resultVo.setData("你好");
        resultVo.setCode("200");
        resultVo.setInfo("哈哈");
       return resultVo;
    }
}
